package com.gzpi.hwgggh.controller;

import com.alibaba.fastjson.JSON;
import com.gzpi.hwgggh.entity.GGP;
import com.gzpi.hwgggh.service.GGPService;
import com.gzpi.hwgggh.sysenum.EnumErrorCode;
import com.gzpi.hwgggh.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @date : 2020-12-18 17:54
 **/
@RequestMapping("/GGP")
@Controller
@CrossOrigin
public class GGPController {

    @Autowired
    private GGPService ggpService;

    @RequestMapping(value = "/scGgps.do",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> scGgps(@RequestBody String jsonData) {
        List<GGP> ggps = JSON.parseArray(jsonData, GGP.class);
        Map<String ,Object> mapList = new HashMap<>();
        /*List<String> list = new ArrayList<>();
        mapList.put("result","succee");
        try {
            for(GGP ggp:ggps){
                Boolean isFa = ggpService.selectGgpByIdisexist(ggp.getID());
                if(isFa){
                    if(!ggpService.updateGgp(ggp)) {
                        mapList.put("result", "error");
                        list.add(ggp.getID());
                    }
                }else {
                    if(!ggpService.addGgp(ggp)) {
                        mapList.put("result", "error");
                        list.add(ggp.getID());
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        mapList.put("data",list);*/
        return mapList;
    }

    /**
     * 编辑广告牌信息
     * @param xzJson 现状广告牌json数据
     * @param ghJson 规划广告牌json数据
     * @param ccJson 拆除广告牌json数据
     * @return
     */
    @RequestMapping("/editGGP.do")
    @ResponseBody
    public Result editGGP(String xzJson,String ghJson,String ccJson,HttpServletRequest request){
        try {
            return ggpService.editGgp(xzJson,ghJson,ccJson,request);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.createError(EnumErrorCode.ERROR_SYSTEM_UNKNOWN.toString());
        }
    }

    @RequestMapping("/editGGCC.do")
    @ResponseBody
    public Result editGGCC(String json,String dataJson, @RequestParam(value = "xzFile",required = false) MultipartFile xzfile,@RequestParam(value = "ghFile",required = false) MultipartFile  ghfile, HttpServletRequest request){
        try {
            return ggpService.editGGCC(json,dataJson,xzfile,ghfile,request);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.createError(EnumErrorCode.ERROR_SYSTEM_UNKNOWN.toString());
        }
    }

    /**
     * 获取新广告牌id
     * @param dwtype
     * @param dwbh
     * @return
     */
    @RequestMapping("/getNewGGPID.do")
    @ResponseBody
    public Result getNewGGPID(String dwtype,String dwbh,String id){
        try {
            return ggpService.getNewGGPID(dwtype,dwbh,id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.createError(EnumErrorCode.ERROR_SYSTEM_UNKNOWN.toString());
        }
    }

}
